package com.z.admin.service.impl;

import com.z.admin.entity.Blog;
import com.z.admin.dao.BlogMapper;
import com.z.admin.service.IBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.z.common.base.Results;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author z
 * @since 2022-04-11
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    @Resource
    private BlogMapper blogMapper;
    @Override
    public Results<Blog> findPageByMap(Map<String, Object> map, Integer offset, Integer limit) {
        StringBuffer where = new StringBuffer("");
        if(map != null && map.size() > 0){
            where.append("where ");
            for(Map.Entry<String, Object> entry : map.entrySet()){
                String[] strArr = entry.getKey().split("/");
                where.append(strArr[0]+" ");
                if(strArr[1].equals("like")){
                    where.append("like '%" +entry.getValue()+"%' and ");
                }else{
                    where.append("= '" +entry.getValue()+"' and ");
                }
            }
            where.delete(where.length()-4, where.length());
        }
        return Results.success(blogMapper.conutByMap(where.toString()), blogMapper.findPageByWhereSql(where.toString(), offset, limit));
    }
}
